package cz.hanis.android.pubaccounting.db.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.provider.BaseColumns;
import cz.hanis.android.pubaccounting.db.tables.ItemTable;
import cz.hanis.android.pubaccounting.db.tables.ItemTable.ItemColumns;
import cz.hanis.android.pubaccounting.model.Item;

public class ItemDao implements Dao<Item> {

	private static final String INSERT = "insert into " + ItemTable.TABLE_NAME
			+ "(" + ItemColumns.NAME + ", " + ItemColumns.ABBREVIATION + ", "
			+ ItemColumns.COST + ", " + ItemColumns.PUB_ID
			+ ") values (?, ?, ?, ?)";

	private SQLiteDatabase db;
	private SQLiteStatement insertStatement;

	public ItemDao(SQLiteDatabase db) {
		this.db = db;
		insertStatement = db.compileStatement(ItemDao.INSERT);
	}

	@Override
	public long save(Item entity) {
		insertStatement.clearBindings();
		insertStatement.bindString(1, entity.getName());
		insertStatement.bindString(2, entity.getAbbreviation());
		insertStatement.bindDouble(3, entity.getCost());
		insertStatement.bindDouble(4, entity.getPubId());
		return insertStatement.executeInsert();
	}

	@Override
	public void update(Item entity) {
		final ContentValues values = new ContentValues();
		values.put(ItemColumns.NAME, entity.getName());
		values.put(ItemColumns.ABBREVIATION, entity.getAbbreviation());
		values.put(ItemColumns.COST, entity.getCost());
		values.put(ItemColumns.PUB_ID, entity.getPubId());
		db.update(ItemTable.TABLE_NAME, values, BaseColumns._ID + " = ?",
				new String[] { String.valueOf(entity.getId()) });
	}

	@Override
	public void delete(Item entity) {
		if (entity.getId() > 0) {
			db.delete(ItemTable.TABLE_NAME, BaseColumns._ID + " = ?",
					new String[] { String.valueOf(entity.getId()) });
		}
	}

	// get field slot error, show how it works by removing a column in the
	// columns array here)
	@Override
	public Item get(long id) {
		Item entity = null;
		Cursor c = db.query(ItemTable.TABLE_NAME, new String[] {
				ItemColumns._ID, ItemColumns.NAME, ItemColumns.ABBREVIATION,
				ItemColumns.COST, ItemColumns.PUB_ID }, ItemColumns._ID
				+ " = ?", new String[] { String.valueOf(id) }, null, null,
				null, "1");
		if (c.moveToFirst()) {
			entity = this.buildEntityFromCursor(c);
		}
		if (!c.isClosed()) {
			c.close();
		}
		return entity;
	}

	@Override
	public List<Item> getAll() {
		List<Item> list = new ArrayList<Item>();
		Cursor c = db.query(ItemTable.TABLE_NAME, new String[] {
				ItemColumns._ID, ItemColumns.NAME, ItemColumns.ABBREVIATION,
				ItemColumns.COST, ItemColumns.PUB_ID }, null, null, null, null,
				ItemColumns.NAME, null);
		if (c.moveToFirst()) {
			do {
				Item entity = this.buildEntityFromCursor(c);
				if (entity != null) {
					list.add(entity);
				}
			} while (c.moveToNext());
		}
		if (!c.isClosed()) {
			c.close();
		}
		return list;
	}

	public List<Item> getItems(long pubId) {
		List<Item> list = new ArrayList<Item>();
		// join movie_category and category, so we can get category name in one
		// query
		String sql = "select " + ItemColumns._ID + ", " + ItemColumns.NAME
				+ ", " + ItemColumns.ABBREVIATION + ", " + ItemColumns.COST
				+ ", " + ItemColumns.PUB_ID + " from " + ItemTable.TABLE_NAME
				+ " where " + ItemColumns.PUB_ID + " = ?";
		Cursor c = db.rawQuery(sql, new String[] { String.valueOf(pubId) });
		if (c.moveToFirst()) {
			do {
				Item entity = this.buildEntityFromCursor(c);
				if (entity != null) {
					list.add(entity);
				}
			} while (c.moveToNext());
		}
		if (!c.isClosed()) {
			c.close();
		}
		return list;
	}

	private Item buildEntityFromCursor(Cursor c) {
		Item entity = null;
		if (c != null) {
			entity = new Item();
			entity.setId(c.getLong(0));
			entity.setName(c.getString(1));
			entity.setAbbreviation(c.getString(2));
			entity.setCost(c.getDouble(3));
			entity.setPubId(c.getInt(4));
		}
		return entity;
	}
}
